package com.zeus.base.smscenter.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.cas.CasFilter;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by ShanFei on 2017-03-08.
 */
public class CustomCasFilter extends CasFilter{
    private static final Logger logger = LoggerFactory.getLogger(CustomFormAuthenticationFilter.class);
    /**
     * 所有请求都会经过的方法
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        return this.executeLogin(request, response);
    }

    /**
     *
     * @param request
     * @param response
     * @param mappedValue
     * @return
     */
    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
        if(null != SecurityUtils.getSubject() || isLoginRequest(request, response)){
            return super.isAccessAllowed(request, response, mappedValue);
        }
        if (ShiroAjaxUtil.isAjax(request)){
            Map<String,String> resultMap = new HashMap<String, String>();
            resultMap.put("success","false");
            resultMap.put("code", "602");
            resultMap.put("message", "\u5F53\u524D\u7528\u6237\u6CA1\u6709\u767B\u5F55\uFF01");//当前用户没有登录！
            ShiroAjaxUtil.out(response, resultMap);
        }
        return Boolean.FALSE;
    }
    /**
     *登陆认证成功
     * @param token
     * @param subject
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @SuppressWarnings("Duplicates")
    @Override
    protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request, ServletResponse response) throws Exception {
        if (!ShiroAjaxUtil.isAjax(request)) {// 不是ajax请求
            issueSuccessRedirect(request, response);
        } else {
            Map<String,String> resultMap = new HashMap<String, String>();
            resultMap.put("success","true");
            resultMap.put("message","登陆成功");
            ShiroAjaxUtil.out(response,resultMap);
        }
        return false;
        //return super.onLoginSuccess(token, subject, request, response);
    }
    /**
     *登陆认证失败
     * @param token
     * @param ae
     * @param request
     * @param response
     * @return
     */
    @SuppressWarnings("Duplicates")
    @Override
    protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException ae, ServletRequest request, ServletResponse response) {
        if (!ShiroAjaxUtil.isAjax(request)) {// 不是ajax请求
            return super.onLoginFailure(token, ae, request, response);
        }
        try {
            response.setCharacterEncoding("UTF-8");
            PrintWriter out = response.getWriter();
            String message = ae.getClass().getSimpleName();
            Map<String,String> resultMap = new HashMap<String, String>();
            if ("IncorrectCredentialsException".equals(message)) {
                resultMap.put("success","false");
                resultMap.put("message","密码错误");
                ShiroAjaxUtil.out(response,resultMap);
            } else if ("UnknownAccountException".equals(message)) {
                resultMap.put("success","false");
                resultMap.put("message","账号不存在");
                ShiroAjaxUtil.out(response,resultMap);
            } else if ("LockedAccountException".equals(message)) {
                resultMap.put("success","false");
                resultMap.put("message","账号被锁定");
                ShiroAjaxUtil.out(response,resultMap);
            } else {
                resultMap.put("success","false");
                resultMap.put("message","未知错误");
                ShiroAjaxUtil.out(response,resultMap);
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return false;
        //return super.onLoginFailure(token, ae, request, response);
    }
}
